package players;

import java.util.ArrayList;

import model.*;

public class PositionalPlayer implements Player 
{

	public static double[][] positionScore =   {{ 100, -20,  10,   5,   5,  10, -20, 100},
				 	  		   		     { -20, -50,  -2,  -2,  -2,  -2, -50, -20},
				 					     {  10,  -2,  -1,  -1,  -1,  -1,  -2,  10},
				 					     {   5,  -2,  -1,  -1,  -1,  -1,  -2,   5},
				 					     {   5,  -2,  -1,  -1,  -1,  -1,  -2,   5},
									     {  10,  -2,  -1,  -1,  -1,  -1,  -2,  10},
									     { -20, -50,  -2,  -2,  -2,  -2, -50, -20},
									     { 100, -20,  10,   5,   5,  10, -20, 100}};

	public boolean move(OthelloBoard board, byte color) 
	{
		// get all possible transitions
		ArrayList<Position> moves = board.getTransitions(color);
		if (moves.size() == 0)
		{
			return false;
		}
		
		// find the best one
		double bestScore = -1000.0f;
		Position bestMove = null;
		for(Position move : moves)
		{
			// transition nr k
			OthelloBoard newState = board.getClone();
			newState.makeTransition(move, color);
			
			// evaluate new state
			double score = evaluate(newState, color);
			
			if(score > bestScore)
			{
				bestScore = score;
				bestMove = move;
			}
		}
		
		// transition to 'best' new state
		board.makeTransition(bestMove, color);
		return true;
	}
	
	public double evaluate(OthelloBoard board, byte color)
	{
		float score = 0;
		for(int i = 0; i < board.size; i++)
			for(int j = 0; j < board.size; j++)
			{
				//add positionScores of own stones
				//subtract positionScores of opponent's stones
				score += board.at(i, j) == Field.opponent(color) ? -positionScore[i][j] :
						 board.at(i, j) == color ? positionScore[i][j] : 0;
			}
		return score;
	}
	
	public String toString()
	{
		return "Positional";
	}
	
	@Override
	public void setExploration(boolean aan) {
		// TODO Auto-generated method stub
		
	}
	
}
